//
//  WCG_O_SingleCollectionViewCell.m
//  Project_B
//
//  Created by 黑黑 on 15/11/25.
//  Copyright © 2015年 Atom_Wang. All rights reserved.
//
#import "WCG_O_SingleCollectionViewCell.h"
#import "UIImageView+WebCache.h"
#import "Masonry.h"
#import "NSString+GXSizeWithText.h"
#import "GXiaNetHandle.h"
#import "GXSpecialClickModel.h"

#import "WCG_O_SingleIndex1TableViewCell.h"
#import "GXSuggestArticelTableViewCell.h"
#import "GXSuggestImageViewTableViewCell.h"
#import "GXProviderDetailTableViewCell.h"

static NSInteger page = 2;
@interface WCG_O_SingleCollectionViewCell ()<DZNEmptyDataSetDelegate, DZNEmptyDataSetSource>

@property (nonatomic, assign) NSInteger page;

@end

@implementation WCG_O_SingleCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createTableView];
        if (self.index == 0) {
            self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
                //加载数据
                [self loadMoreData];
            }];
        }else if (self.index == 1){
            
        }
    }
    return self;
}

- (void)createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.emptyDataSetDelegate = self;
    self.tableView.emptyDataSetSource = self;
    self.tableView.bounces = NO;
    self.tableView.scrollEnabled = NO;
    self.tableView.scrollsToTop = NO;
    self.tableView.showsVerticalScrollIndicator = NO;
    [self.contentView addSubview:self.tableView];
    // index == 0
    [self.tableView registerClass:[GXSuggestArticelTableViewCell class] forCellReuseIdentifier:@"index0ArticleCell"];
    [self.tableView registerClass:[GXSuggestImageViewTableViewCell class] forCellReuseIdentifier:@"index0ImageCell"];
    [self.tableView registerClass:[GXProviderDetailTableViewCell class] forCellReuseIdentifier:@"index0ProCell"];
    // index == 1
    [self.tableView registerClass:[WCG_O_SingleIndex1TableViewCell class] forCellReuseIdentifier:@"index1cell"];
    // index == 2
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"index2cell"];
}

#pragma mark - MJRefresh
- (void)loadMoreData {
    __block NSMutableArray *item_TempArr = [NSMutableArray array];
    [GXiaNetHandle getUrl:[NSString stringWithFormat:kSpecialDetailWeb, self.specialModel.special_id, (long)page++] cookie:kSpecialDetailCookie success:^(id result) {
        NSDictionary *dic = result;
        //specialClickModel 里的 item_list 里装的是推荐model
        GXSpecialClickModel *specialClickModel = [GXSpecialClickModel modelWithDic:dic];
        
        //将字典转模型装到数组里/推荐
        item_TempArr = [GXSuggestModel transformWithArray:specialClickModel.item_list];
        
        [self.itemArr addObjectsFromArray:item_TempArr];
        //刷新视图
        if (self.itemArr.count) {
            [self.tableView reloadData];
        }
        //结束加载并提示没有新数据
        [self.tableView.mj_footer endRefreshing];
    } failure:^(NSError *error) {
        
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.index == 0) {
        NSLog(@"❤️!~~~~~%@",self.itemArr);
        return self.itemArr.count;
    }else if (self.index == 1){
        return self.cpArr.count;
    }else
    {
        return 5;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.index == 0) {
        GXSuggestArticelTableViewCell *articleCell = [tableView dequeueReusableCellWithIdentifier:@"index0ArticleCell"];
        
        GXSuggestImageViewTableViewCell *imageViewCell = [tableView dequeueReusableCellWithIdentifier:@"index0ImageCell"];
        
        GXProviderDetailTableViewCell *specialCell = [tableView dequeueReusableCellWithIdentifier:@"index0ProCell"];
        id temp = self.itemArr[indexPath.row];
        if ([temp isKindOfClass:[GXSuggestModel class]]) {
            GXSuggestModel *suggestModel = (GXSuggestModel *)temp;
            //九宫格 大型画册 视频
            if (suggestModel.type.integerValue == 1 || suggestModel.type.integerValue == 2 || suggestModel.type.integerValue == 9) {
                articleCell.suggestModel = self.itemArr[indexPath.row];
                return articleCell;
            }
            //文章
            else if (suggestModel.type.integerValue == 0) {
                //推荐文章
                if (suggestModel.is_recommended == true) {
                    articleCell.suggestModel = self.itemArr[indexPath.row];
                    return articleCell;
                }
                //不推荐文章
                else {
                    specialCell.suggestModel = self.itemArr[indexPath.row];
                    return specialCell;
                }
            }
            else {
                imageViewCell.suggestModel = self.itemArr[indexPath.row];
                return imageViewCell;
            }
        }
    }else if (self.index == 1){
        WCG_O_SingleIndex1TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"index1cell"];
        cell.proModel = self.cpArr[indexPath.row];
        return cell;
    }else{
        if (indexPath.row == 0) {
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"index2cell"];
            cell.textLabel.text = @"即时通讯";
            cell.imageView.image = [UIImage imageNamed:@"contact"];
            return cell;
        }else if(indexPath.row == 1){
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"index2cell"];
            cell.textLabel.text = @"查看地图";
            cell.imageView.image = [UIImage imageNamed:@"map"];
            return cell;
        }else{
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"index2cell"];
            cell.textLabel.text = @"";
            return cell;
        }
    }
    return nil;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (self.tableView == scrollView) {
        CGFloat y = self.tableView.contentOffset.y;
        
        if (y > self.lastTableViewOffset) {
            //NSLog(@"向上");
            if (self.tableView.contentOffset.y == 0) {
                self.tableView.scrollEnabled = NO;
            } else {
                self.tableView.scrollEnabled = YES;
            }
            
        } else {
            //NSLog(@"向下");
            if (self.tableView.contentOffset.y == 0) {
                self.tableView.scrollEnabled = NO;
            } else {
                self.tableView.scrollEnabled = YES;
            }
        }
        
        self.lastTableViewOffset = self.tableView.contentOffset.y;
    }
}
#pragma mark - tableViewcell高度

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (self.index == 0) {
        id temp = self.itemArr[indexPath.row];
        if ([temp isKindOfClass:[GXSuggestModel class]]) {
            GXSuggestModel *suggestModel = (GXSuggestModel *)temp;
            //文本宽度
            CGFloat titleW = self.tableView.frame.size.width - 25 - [UIImage imageNamed:@"icon-image-album"].size.width;
            //标题文本高度
            CGRect titleSize = [suggestModel.title SizeWithTextFontSize:kSuggestTitleFontSize textWidth:titleW];
            //副标题文本高度
            CGRect subTitleSize = [suggestModel.content[@"desc"] SizeWithTextFontSize:KSuggestIntroFontSize textWidth:titleW];
            
            //音乐/大型画册/九宫格
            if (suggestModel.type.integerValue == 10 || suggestModel.type.integerValue == 9 || suggestModel.type.integerValue == 1 || suggestModel.type.integerValue == 2) {
                //返回行高
                return  330 + titleSize.size.height + subTitleSize.size.height + 0.1;
            }
            //文章
            else if (suggestModel.type.integerValue == 0) {
                //推荐文章与不推荐文章返回高度
                return suggestModel.is_recommended == false ? 160 : 330 + titleSize.size.height + subTitleSize.size.height + 0.1;
            }
            //诗歌
            else {
                return 350;
            }
        }
    }else if (self.index == 1)
    {
        return 110;
    }else
    {
        return 50;
    }
    return 0;
}

#pragma mark - tableView 的点击跳转方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (self.index == 0) {
        [self.contentDelegate pushContentWithModel:self.itemArr[indexPath.row]];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"content" object:nil userInfo:@{@"index":[NSNumber numberWithInteger:self.index], @"content":self.itemArr[indexPath.row]} ];
    }if (self.index == 1) {
        [self.proDelegate pushContentWithModel:self.cpArr[indexPath.row]];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"pro" object:nil userInfo:@{@"pro":self.cpArr[indexPath.row], @"index":[NSNumber numberWithInteger:self.index]}];
    }if (self.index == 2) {
        if (indexPath.row == 0) {
            [[NSNotificationCenter defaultCenter] postNotificationName:@"chat" object:nil];
        }
        else if (indexPath.row == 1){
            [[NSNotificationCenter defaultCenter] postNotificationName:@"map" object:nil];
        }
    }
}

#pragma mark - 当数据为空时显示的图片
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
{
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    return [UIImage imageNamed:@"icon-search-article"];
}

#pragma mark - 当数据为空时显示的字符
-(NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
{
    NSString *text = @"数据加载中~";
    NSDictionary *str = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:16.0f]};
    return [[NSAttributedString alloc] initWithString:text attributes:str];
}

@end

